import 'package:fluro/fluro.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_wan_android/entity/article_entity.dart';
import 'package:flutter_wan_android/ui/page/article/article_detail_page.dart';
import 'package:flutter_wan_android/ui/page/setting_page.dart';
import 'package:flutter_wan_android/ui/page/tab/tab_navigator.dart';
import 'package:flutter_wan_android/ui/page/two_level/two_level_page.dart';
import 'package:flutter_wan_android/ui/sign_in/sign_in.dart';

class RoutesManager {
  /// 首页地址
  static const String indexPage = '/tab_navigator';

  /// 页面标识常量
  final String _twoLevelPage = '/two_level_page';
  final String _articleDetailPage = '/article_detail_page';
  final String _loginPage = '/login_page';
  final String _settingPage = '/setting_page';

  ///路由对象
  FluroRouter _fluroRouter;
  static RoutesManager _instance;

  ///
  /// 单例模式
  ///
  static RoutesManager getInstance() {
    if (_instance == null) {
      _instance = new RoutesManager();
      _instance._fluroRouter = FluroRouter();
      _instance._configureRoutes();
    }
    return _instance;
  }

  ///
  /// 路由对象的getter
  ///
  FluroRouter get router => _fluroRouter;

  ///
  /// 获取RouterGenerator
  ///
  RouteFactory getRouterGenerator() {
    return _fluroRouter.generator;
  }

  ///
  /// 初始化配置
  ///
  void _configureRoutes() {
    ///HomePage
    _fluroRouter.define(indexPage, handler: Handler(handlerFunc: (context, Map<String, List<String>> params) {
      return TabNavigator();
    }));

    ///二楼页面
    _fluroRouter.define(_twoLevelPage, handler: Handler(handlerFunc: (context, Map<String, List<String>> params) {
      return TwoLevelPage();
    }));

    ///
    /// 文章详情页
    /// 用来演示传递 对象参数
    ///
    _fluroRouter.define(_articleDetailPage, handler: Handler(handlerFunc: (context, Map<String, List<String>> params) {
      final articleEntity = context.settings.arguments as ArticleEntity;
      return ArticleDetailPage(
        article: articleEntity,
      );
    }));

    ///登录页面
    _fluroRouter.define(_loginPage, handler: Handler(handlerFunc: (context, Map<String, List<String>> params) {
      return SignIn();
    }));

    ///设置页面
    _fluroRouter.define(_settingPage, handler: Handler(handlerFunc: (context, Map<String, List<String>> params) {
      return SettingPage();
    }));

    ///
    ///page3
    ///用来演示传递 基本类型
    ///
    // _fluroRouter.define("$_page3/:${Page3.ARG_NAME}", handler:
    // Handler(handlerFunc: (context, Map<String, List<String>> params) {
    //   final arg = params[Page3.ARG_NAME].first;
    //   return Page3(arg);
    // }));
  }

  ///
  /// 出栈
  ///
  void pop<T>(BuildContext context, [T result]) {
    _fluroRouter.pop(context, result);
  }

  ///
  /// 跳转到TwoLevel
  ///
  Future pushTwoLevelPage(BuildContext context) {
    return _fluroRouter.navigateTo(context, _twoLevelPage, //跳转路径
        transition: TransitionType.inFromTop);
  }

  ///
  /// 跳转到详情页
  ///
  Future pushArticleDetailPage(BuildContext context, ArticleEntity articleEntity) {
    return _fluroRouter.navigateTo(
      context, _articleDetailPage, //跳转路径
      rootNavigator: false,
      transition: TransitionType.inFromRight,
      routeSettings: RouteSettings(
        name: "article_entity",
        arguments: articleEntity,
      ),
    );
  }

  ///
  /// 跳转到登录页面
  ///
  Future pushLoginPage(BuildContext context) {
    return _fluroRouter.navigateTo(context, _loginPage, //跳转路径
        transition: TransitionType.inFromBottom);
  }


  ///
  /// 跳转到设置页面
  ///
  Future pushSettingPage(BuildContext context) {
    return _fluroRouter.navigateTo(context, _settingPage, //跳转路径
        transition: TransitionType.inFromRight);
  }
//
// ///
// /// 跳转到page1
// ///
// void pushPage3(BuildContext context, String arg) {
//   _fluroRouter.navigateTo(context, "$_page3/$arg");
// }
}
